<?php

/********************* body() *************************************************/
$previousURL = substr($_SERVER['HTTP_REFERER'], 
			   strrpos($_SERVER['HTTP_REFERER'],'/') + 1);
$userInput = "";
$searchType = "";

// user input from the search bar
if(isset($_GET['searchInput']) && strlen($_GET['searchInput']) != 0){
	$userInput = $_GET['searchInput'];
	$searchType = "searchInput";
}

// user input from the search field, including zip, city, and vineyard
else if(isset($_GET['zipInput']) && strlen($_GET['zipInput']) != 0){
	$userInput = $_GET['zipInput'];
	$searchType = "zipInput";
}
else if(isset($_GET['cityInput']) && strlen($_GET['cityInput']) != 0){
	$userInput = $_GET['cityInput'];
	$searchType = "cityInput";
}
else if(isset($_GET['vineyardInput']) && strlen($_GET['vineyardInput']) != 0){
	$userInput = $_GET['vineyardInput'];
	$searchType = "vineyardInput";
}

threatCheck($previousURL, $userInput);
isEmptyCheck($previousURL, $userInput);
zipCodeCheck($previousURL, $userInput, $searchType);
cityNameCheck($previousURL, $userInput, $searchType);

/********************* end of body() ******************************************/



/********************* threatCheck() ******************************************/
function threatCheck($previousURL, $userInput){
	$initialLength = strlen($userInput);
	$searchString = strip_tags($userInput);
	$length1 = strlen($searchString);
	if($initialLength != $length1){
		header('Location: ../'. $previousURL . '');
		break;
	}
}
/********************* isEmpty() **********************************************/
function isEmptyCheck($previousURL, $userInput){
	$length = strlen(preg_replace('/\s+/', '', $userInput));
	if($length == 0){
		header('Location: ../'. $previousURL . '');
		break;
	}
}
/********************* zipCodeCheck() *****************************************/
function zipCodeCheck($previousURL, $userInput, $searchType){
	$int = "(^[0-9]{1}\b)";	
	$intPattern = "(^[0-9]{5}\b)";
	$firstChar = substr($userInput, 0, 1);	
	
	if(preg_match($int, $firstChar)) {
		if(preg_match($intPattern, $userInput)){
			if($searchType == "zipInput"){
				header('Location: ../search.php?zipInput=' . 
						$userInput . '&length=5');
			}
			else if($searchType == "vineyardInput"){
				header('Location: ../'. $previousURL . '');
			}
			else if($searchType == "cityInput"){
				header('Location: ../'. $previousURL . '');
			}
			else if($searchType == "searchInput"){
				header('Location: ../search.php?zipCode=' . 
						$userInput . '&length=5');
			}
			break;
		}
		else{				
			header('Location: ../'. $previousURL . '');
		}
	}
	else{
		header('Location: ../'. $previousURL . '');
	}	
}
/********************* cityNameCheck() ****************************************/
function cityNameCheck($previousURL, $userInput, $searchType){
	$str = "(^[a-zA-Z]{1}\b)";
	$firstChar = substr($userInput, 0, 1);

	$stateLibrary = array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
						  "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", 
						  "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", 
						  "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", 
						  "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", 
					 	  "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", 
					 	  "WI", "WY");
					 	  
	$strLen = strlen($userInput);
	$pureStr = preg_replace('/\s+/', '', $userInput);
	$length = strlen($pureStr);	
	$strPattern = "(^[a-zA-Z]{" . $length . "}\b)";

	if($length == 2 && $searchType == "searchInput"){
		for($i = 0; $i < sizeof($stateLibrary); $i++){
			if(strtolower($pureStr) == strtolower($stateLibrary[$i])){
				header('Location: ../search.php?stateAbb=' .
						$userInput . '&length=' . $strLen);
				break;
			}
		}
	}
	else if(preg_match($str, $firstChar)){
		if(preg_match($strPattern, $pureStr)){
			if($searchType == "vineyardInput"){
				header('Location: ../search.php?vineyardInput=' . 
						$userInput . '&length=' . $strLen);
			}
			else if($searchType == "zipInput"){
				header('Location: ../'. $previousURL . '');
			}
			else if($searchType == "cityInput"){
				header('Location: ../search.php?cityInput=' . 
						$userInput . '&length=' . $strLen);
			}
			else if($searchType == "searchInput"){
				header('Location: ../search.php?cityName=' . 
						$userInput.'&length=' . $strLen);
			}
			break;
		}
	}
}

?>
